{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('./chi_sim.txt') as f:\n",
    "    chi_sim = [w[0] for w in f.readlines()[:3500]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cleanData(dirname, txtname, chi_sim):\n",
    "    paths = glob(os.path.join(dirname, '*.txt'))\n",
    "    with open(txtname, 'w') as F:\n",
    "        for path in tqdm(paths):\n",
    "            with open(path) as f:\n",
    "                txt = f.read()\n",
    "                txt = ''.join([w for w in txt if w in chi_sim + ['\\n']])\n",
    "                txt = txt.replace('\\n', ' ')\n",
    "                seg = ' '.join(jieba.lcut(txt))\n",
    "                seg = ' '.join(seg.split())\n",
    "                F.write(seg + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/70 [00:00<?, ?it/s]Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 0.816 seconds.\n",
      "Prefix dict has been built succesfully.\n",
      "100%|██████████| 70/70 [00:01<00:00, 38.31it/s]\n",
      "100%|██████████| 70/70 [00:01<00:00, 50.08it/s]\n"
     ]
    }
   ],
   "source": [
    "cleanData('./train/0/', '0.txt', chi_sim)\n",
    "cleanData('./train/1/', '1.txt', chi_sim)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
